<?php
namespace app\common\logic;

use think\Db;
use think\Model;

class Carousel extends Base
{
    public function lists($pm = [])
    {
        if (!empty($pm['title'])) {
            $where['title'] = ['like', "%{$pm['title']}%"];
        }
        $where['is_delete'] = 0;
        $list = Db::name('carousel')->where($where)->order('orders asc,id desc')->select();
        foreach ($list as &$item)
        {
            if (!strpos($item['link'],'http') === 0) {
                $item['link'] = SITE_WEB.$item['link'];
            }
            if (!strpos($item['pic_path'],'http') === 0) {
                $item['pic_path'] = SITE_WEB.$item['pic_path'];
            }
        }
        $data['list'] = $list;
        $data['count'] = Db::name('carousel')->where($where)->count();
        return $data;
    }

    public function detail($pm)
    {
        $single = Db::name('carousel')->where('id', $pm['id'])->find();
        return $single;
    }

    public function add($pm)
    {
        $pm['create_time'] = time();
        return Db::name('carousel')->strict(false)->insert($pm);
    }

    public function edit($pm)
    {
        return Db::name('carousel')->strict(false)->update($pm);
    }

    public function del($pm){
        return Db::name('carousel')->where('id',$pm['id'])->update(['is_delete'=>1]);
    }

}